package com.android.mg.tgaazul;

import com.android.mg.db.DBAdapter;
import com.android.mg.db.DBAdapter.LocalBinder;

import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;

public class HomeActivity extends Activity {

	//Declaramos las Propiedas
	private String[] mNavigationDrawerItemTitles;
	private DrawerLayout mDrawerLayout;
	private ListView mDrawerList;
	
	private ActionBarDrawerToggle mDrawerToggle;
	
	private CharSequence mDrawerTitle;
	
	private CharSequence mTitle;
	
	// Variables para la BD SQLITE 
	DBAdapter dbAdapter;
	Boolean mBound;
	
		
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        
        // seteamos las propiedades
        mTitle = mDrawerTitle = getTitle();
        
        // inicializamos las propiedades
        mNavigationDrawerItemTitles =getResources().getStringArray(R.array.menu_array);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.left_drawer);
        
        //Listamos los items
        ObjectDrawerItem[] drawerItem = new ObjectDrawerItem[3];
        
        drawerItem[0] = new ObjectDrawerItem(R.drawable.ic_action_paraderos, "Paraderos");
        drawerItem[1] = new ObjectDrawerItem(R.drawable.ic_action_about, "Informacion");
        drawerItem[2] = new ObjectDrawerItem(R.drawable.ic_action_place, "Mapa");
        
        //Pasamos la informacion a la lista ListView adapter
        DrawerItemCustomAdapter adapter = new DrawerItemCustomAdapter(this,R.layout.drawer_list_item, drawerItem);
        
        mDrawerList.setAdapter(adapter);
        
        // seteamos el evento click listener del item seleccionado
        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
        
        // para la app icon control nav
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerToggle = new ActionBarDrawerToggle(
        		this,
        		mDrawerLayout,
        		R.drawable.ic_drawer,
        		R.string.drawer_open,
        		R.string.drawer_close
        		){
        	
        	public void onDrawerClosed(View view)
        	{
        		super.onDrawerClosed(view);
        		getActionBar().setTitle(mTitle);
        	}
        	
        	public void onDrawerOpened(View drawerView)
        	{
        		super.onDrawerOpened(drawerView);
        		getActionBar().setTitle(mDrawerTitle);
        	}
        };
        
        mDrawerLayout.setDrawerListener(mDrawerToggle);
        
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);
        
        if (savedInstanceState == null)
        {
        	selectItem(0);
        }
                      
     
       
    }
  

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.home, menu);
		return true;
	}


	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		
		if(mDrawerToggle.onOptionsItemSelected(item)){
			return true;
		}
		
		return super.onOptionsItemSelected(item);
	}


	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onPostCreate(savedInstanceState);
		mDrawerToggle.syncState();
	}
	
	
    private class DrawerItemClickListener implements ListView.OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			
			selectItem(position);			
		}
    	
    }
    
    private void selectItem(int position)
    {
    	
    	Fragment fragment = null;
    	
    	switch (position) {
		case 0:
			fragment = new ParaderosFragment();
			break;
			
		case 1:
			fragment = new InformacionFragment();
			break;
			
		case 2:
			fragment = new MapaFragment();
			break;
		default:
			break;
		}
    	
    	if(fragment != null)
    	{
    		FragmentManager fragmentManager = getFragmentManager();
    		fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
    		
    		mDrawerList.setItemChecked(position, true);
    		mDrawerList.setSelection(position);
    		setTitle(mNavigationDrawerItemTitles[position]);
    		mDrawerLayout.closeDrawer(mDrawerList);
    	}else{
    		Log.e("HomeActivity", "Error in creating fragment");
    	}
    	
    	
    }


	@Override
	public void setTitle(CharSequence title) {
		mTitle = title;
		getActionBar().setTitle(mTitle);
	}


	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		
		// Enlazamos con el Servicio
		Intent intent = new Intent(this, DBAdapter.class);
		bindService(intent, mConnection, Context.BIND_AUTO_CREATE);		
		Toast.makeText(this, "Iniciando servicio", Toast.LENGTH_LONG);
	}


	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		// Nos desenlazamos del servicio
		if (mBound) {
			unbindService(mConnection);
			mBound = false;
		}
	}
	
	
	private ServiceConnection mConnection = new ServiceConnection() {
		
		@Override
		public void onServiceDisconnected(ComponentName name) {
			// TODO Auto-generated method stub
			mBound = false;
		}
		
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			// TODO Auto-generated method stub
			LocalBinder binder = (LocalBinder) service;
			dbAdapter = binder.getService();
			mBound = true;		
						
		}
	};
	
	
	
	
}
